﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;
using Simple.Services.System.QHSEUser.Models;
using Simple.Services.System.QHSEViolationCategory.Models;

namespace Simple.Services;

public class QHSEDangerClauseAssocitionService
{
    private readonly SimpleDbContext _context;
    private readonly QHSEDangerCategoryService _categoryService;
    private readonly QHSEDangerCategoryHSEService _dangerCategoryHSEService;
    public QHSEDangerClauseAssocitionService(SimpleDbContext context, QHSEDangerCategoryService categoryService,QHSEDangerCategoryHSEService dangerCategoryHSEService)
    {
        _context = context;
        _categoryService = categoryService;
        _dangerCategoryHSEService = dangerCategoryHSEService;
    }
    public async Task<List<QHSEDangerClauseAssocitionModel>> GetAsync()
    {
        var violationCategories = await _context.Set<QHSESysDangerClauseAssocition>().AsNoTracking().ToListAsync();
        return MapperHelper.Map<List<QHSEDangerClauseAssocitionModel>>(violationCategories);
    }
    public async Task<DangerClauseResultModel> GetClauseResult(IdInputModel idInput)
    {
        var clauseAssocition = await _context.Set<QHSESysDangerClause>().AsNoTracking()
            .FirstOrDefaultAsync(u => idInput.Id.Equals(u.GeneralSpecialtyId)); ;

        if (clauseAssocition == null)
        {
            return null;
        }
        var dangerType = await _context.Set<QHSESysDictionary>().AsNoTracking().Where(u => u.Code == clauseAssocition.DangerTypeCode).Select(u => u.Item).FirstOrDefaultAsync();
        var dangerClauserResult = new DangerClauseResultModel
        {
            DangerType = dangerType,
            DangerTypeCode = clauseAssocition.DangerTypeCode,
            DangerTypeCategory = clauseAssocition.Note
        };

        return dangerClauserResult;
        /* var category = await _categoryService.GetByCodeAsync(clauseAssocition.DangerCategoryCode);
         var hseResultTask = _dangerCategoryHSEService.GetResult(new QHSEDangerCategoryHSEPageInputModel { Id = category.Id });

         await Task.WhenAll(hseResultTask);

         var clauseResultModel = new DangerClauseResultModel
         {
             DangerCategory = category.Item,
             HSE = hseResultTask.Result.Result
         };
         clauseResultModel.DangerType =await _context.Set<QHSESysDictionary>().AsNoTracking().Where(u=>u.Code ==clauseAssocition.DangerTypeCode).Select(u=>u.Item).FirstOrDefaultAsync();
         clauseResultModel.DangerTypeCategory = await _context.Set<QHSESysDictionary>().AsNoTracking().Where(u => u.Code == clauseAssocition.ClauseTypeCode).Select(u => u.Item).FirstOrDefaultAsync();
         return clauseResultModel;*/
    }

}
